home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_brickbump3.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  4.0 KB  |  117 lines

  1. /*
  2.  * brickbump.sl -- displacement shader for bricks.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes displacements for a wall of bricks.  This is the companion
  6.  *   shader to the surface "brick" shader.  The parameters work exactly
  7.  *   the same.  Of course, you can use it with any surface shader, and
  8.  *   in fact matte or plastic gives those nice white cinder block walls.
  9.  *   However, if you do use it with "brick", the parameters MUST match,
  10.  *   or your bricks will look very strange.
  11.  * 
  12.  * PARAMETERS:
  13.  *    brickwidth                Width of a brick (in st space)
  14.  *    brickheight               Height of a brick (in st space)
  15.  *    mortarthickness           Thickness of the mortar (in st space)
  16.  *    rowvary                   How much does each row shift?
  17.  *    jagged                    How much do bricks deviate from squares?
  18.  *    pitting                   The amplitude of the "pits" on the face of
  19.  *                                 the bricks.
  20.  *    pockfrequency             The st frequency of the pits.
  21.  *    groovedepth               The depth of the grooves between bricks.
  22.  *
  23.  * AUTHOR: written by Larry Gritz, 1992
  24.  *
  25.  * HISTORY:
  26.  *      28 May 1992 -- written by lg for the "Timbre Trees" video (saucer)
  27.  *      12 Jan 1994 -- recoded by lg in correct shading language.
  28.  *
  29.  * last modified  12 Jan 1994 by Larry Gritz
  30.  */
  31.  
  32. /* note from Larry:
  33.  
  34.   You may note the companion shaders "brick" and "brickbump".
  35.  
  36.   They're meant to go together, but I like to use the brickbump
  37.   displacement shader with the matte surface shader.  With appropriate
  38.   parameters, it looks *exactly* like those painted cinderblock walls
  39.   (we have a wall in our lab like this).  
  40.   ~gritz/brick.tif.
  41.  
  42.   I think good parameters for this look are:
  43.  
  44.   Displacement "brickbump" "brickwidth" 0.5 "brickheight" 0.25 
  45.            "mortarthickness" 0.02 "pitting" 0.015 "pockfrequency" 12
  46.               "groovedepth" 0.015
  47. */
  48.  
  49. displacement
  50. k3d_brickbump3 ( float jagged = 0.006;
  51.             float brickwidth = .25, brickheight = .08;
  52.         float mortarthickness = .01;
  53.         float rowvary = .25, pitting = 0.01;
  54.         float pockfrequency = 10, groovedepth = 0.01; )
  55. {
  56. #define BMWIDTH (brickwidth+mortarthickness)
  57. #define BMHEIGHT (brickheight+mortarthickness)
  58. #define MWF (mortarthickness*0.5/BMWIDTH)
  59. #define MHF (mortarthickness*0.5/BMHEIGHT)
  60. #define snoise(x) (2 * noise((x)) - 1)
  61. #define boxstep(a,b,x) (clamp(((x)-(a))/((b)-(a)),0,1))
  62. #define sqr(x) ((x)*(x))
  63.   point PP2;
  64.   float sbrick, tbrick, w, h;
  65.   float scoord, tcoord, ss, tt;
  66.   float fact, disp;
  67.  
  68.   scoord = s;  tcoord = t;
  69.  
  70.   /* Make the shapes of the bricks vary just a bit */
  71.   PP2 = point noise (s/BMWIDTH, t/BMHEIGHT);
  72.   scoord = s + jagged * xcomp (PP2);
  73.   tcoord = t + jagged * ycomp (PP2);
  74.  
  75.   ss = scoord / BMWIDTH;
  76.   tt = tcoord / BMHEIGHT;
  77.  
  78.   /* shift alternate rows */
  79.   if (mod (tt*0.5, 1) > 0.5)
  80.       ss += 0.5;
  81.  
  82.   tbrick = floor (tt);   /* which brick row? */
  83.   /* Shift the columns randomly by row */
  84.   ss += rowvary * (noise (tbrick+0.5) - 0.5);
  85.  
  86.   sbrick = floor (ss);   /* which brick column? */
  87.   ss -= sbrick;          /* Now ss and tt are coords within the brick */
  88.   tt -= tbrick;
  89.  
  90.   fact = 1;
  91.   disp = 0;
  92.   if (tt < MHF) {
  93.       /* We're in the top horizontal groove */
  94.       disp = groovedepth * (sqr((tt)/MHF) - 1);
  95.     }
  96.   if (tt > (1.0-MHF)) {
  97.       /* Bottom horizontal groove */
  98.       disp = groovedepth * (sqr((1-tt)/MHF) - 1);
  99.     }
  100.   if (ss < MWF) {
  101.       disp = 0.75 * groovedepth * (sqr(ss/MWF) - 1);
  102.     }
  103.   if (ss > (1.0-MWF)) {
  104.       disp = 0.75 * groovedepth * (sqr((1-ss)/MWF) - 1);
  105.     }
  106.  
  107.   fact = smoothstep (0, 1.3*MHF, tt) - smoothstep (1.0-1.3*MHF, 1, tt);
  108.   fact *= (smoothstep (0, 1.3*MWF, ss) - smoothstep (1.0-1.3*MWF, 1, ss));
  109.   fact = pitting * (0.75 * fact + 0.25);
  110.   disp -= fact * pow(noise ((ss+sbrick)*pockfrequency/BMHEIGHT,
  111.                     (tt+tbrick)*pockfrequency/BMWIDTH), 0.25);
  112.  
  113.   P += disp * normalize(N);
  114.   N = calculatenormal (P);
  115.  
  116. }
  117.